var express = require('express');
var router = express.Router();
var request = require('request');
var md5 = require('md5');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// 轮播图 列表
router.get('/jkyx/pc/banners_list', function(req, res, next) {

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_banners_list,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '登录成功',
	     			status:'100',
	     			banners: results
	     		}));
			}
		})
	})
});
// 新建轮播图
router.get('/jkyx/pc/banners_new', function(req, res, next) {
	var param = req.query || req.params; 
	// 轮播图类型  1：商品 2：公告
	var type = param.type || 1;
	// 轮播图类型  图片地址
	var image_url = param.image_url || '';
	// 商品ID
	var goods_id = param.goods_id || '';
	// 时间
	var create_time = CommonJS.getNowFormatDate();
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_banners_new,[type,image_url,goods_id,create_time],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			banners: results[1]
	     		}));
			}
		})
	})
});

// 删除轮播图
router.get('/jkyx/pc/banners_delete', function(req, res, next) {
	var param = req.query || req.params; 
	// 轮播图 ID
	var id = param.id || 1;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_banners_delete,[id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			banners: results[1]
	     		}));
			}
		})
	})
});

module.exports = router;
